import { Handler } from "./Handler";
import { any } from "../../../core/logic";
import { Channel, Sampler} from "../../../component/animation/index";
import { Vector2 } from "../../../core/math/Vector2";
class RotateMouse extends Handler{
    constructor(opts={}){
        super(opts);
        this.button=any(opts.rotateButton,2);
        this.free=any(opts.free,false);//是否自由旋转，缺省false，在pitch和roll只能同时进行一种。
        this.easing=any(opts.easing,"easeOut_Expo");
        this.dual=any(opts.dual,0.5);//1s
        this.speed=any(opts.speed,0.2);
        this.maxPitch=any(opts.maxPitch,Number.MAX_VALUE);//最大倾斜角
        this.cur=null;//当前值
        this.pre=null;
        this.dir=0;//旋转的方向
        this.autoRoll=any(opts.autoRoll,true);
        this.register();
    }
    register(){
        this.on("mousedown",this.start);
        this.on("mousemove",this.update);
        this.on("mouseup",this.end);
    }
    start(event){
        if(!this.avalid){
            return;
        }
        this.animation.stop();
        this.animation.clear();
        if(event.data.button===this.button){
            this.alive=true;
            this.cur=this.pre={
                pixel:event.data.pixel,
                time:event.data.time
            }
        }
        this.dir=0;
        if(event.originEvent.ctrlKey){
            if(!this.achor){
                this.achor=event.data.pixel;
            }else{
                this.achor=null;
            } 
        }
        
    }
    update(event){
        if(!this.alive){
            return;
        }
        this.pre=this.cur;
        this.cur={
            pixel:event.data.pixel,
            time:event.data.time
        }
        //鼠标在屏幕的上半部分还是下半部分
        if(this.dir==0){
            if(this.autoRoll){
                this.dir=Math.sign(this.pre.pixel[1]-this.target.size[1]/2)*Math.sign(this.target.trans.position.z-this.target.at.z);
            }else{
                this.dir=1;
            }
            
        }
        let pitch=-(this.cur.pixel[1]-this.pre.pixel[1])/360;
        let roll=-(this.cur.pixel[0]-this.pre.pixel[0])*this.dir/360;
        if(this.achor){
            roll/=5;
        }
        //同时生效
        if(this.free){
            this.target.roll+=roll;
            this.target.pitch=Math.max(0,Math.min(this.target.pitch+pitch,this.maxPitch));
        }else{
            if(Math.abs(roll)>=Math.abs(pitch)){//绕z轴旋转 roll
                this.target.roll+=roll;
            }else{
                this.target.pitch=Math.max(0,Math.min(this.target.pitch+pitch,this.maxPitch));
            }
        }
        if(this.achor){//shift按住绕鼠标点旋转
            let pos=this.target.pixelToWorld(this.achor);
            let c=new Vector2(pos.x,pos.y);
            let v=new Vector2(this.target.at.x,this.target.at.y);
            v.rotateAround(c,roll);
            this.target.at=[v.x,v.y,0];
        }
        
    }
    end(event){
        if(!this.alive){
            return false;
        }
        this.alive=false;
        if(this.dual<=0){
            return false;
        }
        const t=this.cur.time-this.pre.time;
        if(t==0){
            return false;
        }
        //鼠标垫在屏幕的上半部分还是下半部分
        //let xx=Math.sign(this.pre.pixel[1]-this.target.size[1]/2)*Math.sign(this.target.trans.position.z-this.target.at.z);
        let pitch=-(this.cur.pixel[1]-this.pre.pixel[1])/360;
        let roll=-(this.cur.pixel[0]-this.pre.pixel[0])*this.dir/360;
        if(this.achor){
            roll/=5;
        }
        if(this.free){
            let s=roll/t*this.dual*this.speed/2;
            let sampler=new Sampler([0,this.dual],[this.target.roll,this.target.roll+s]);
            let channel=new Channel(this.target,"roll");
            channel.add(sampler);
            s=pitch/t*this.dual*this.speed/2;
            let sampler1=new Sampler([0,this.dual],[this.target.pitch,Math.max(0,Math.min(this.target.pitch+s,this.maxPitch))]);
            let channel1=new Channel(this.target,"pitch");
            channel1.add(sampler1);
            this.animation.add(channel);
            this.animation.add(channel1);
        }else{
            if(Math.abs(roll)>=Math.abs(pitch)){//绕z轴旋转 roll
                let s=roll/t*this.dual*this.speed/2;
                let sampler=new Sampler([0,this.dual],[this.target.roll,this.target.roll+s]);
                let channel=new Channel(this.target,"roll");
                channel.add(sampler);
                this.animation.add(channel);
            }else{
                let s=pitch/t*this.dual*this.speed/2;
                let sampler=new Sampler([0,this.dual],[this.target.pitch,Math.max(0,Math.min(this.target.pitch+s,this.maxPitch))]);
                let channel=new Channel(this.target,"pitch");
                channel.add(sampler);
                this.animation.add(channel);
            }
        }
        if(this.achor){//shift按住绕鼠标点旋转
            let pos=this.target.pixelToWorld(this.achor);
            let c=new Vector2(pos.x,pos.y);
            let v=new Vector2(this.target.at.x,this.target.at.y);
            v.rotateAround(c,roll);
            let sampler2=new Sampler([0,this.dual],[this.target.at.toArray(),[v.x,v.y,0]]);
            let channel2=new Channel(this.target,"at");
            channel2.add(sampler2);
            this.animation.add(channel2);
        }
        this.animation.start(0);     
    }
}
export {RotateMouse}